﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Common;

namespace Extensions.Consul;

public class ConsulHelper
{
    private readonly HttpHelper _httpHelper;
    private readonly IConsulServerManager _consulServerManager;

    public ConsulHelper(HttpHelper httpHelper,IConsulServerManager consulServerManager)
    {
        _httpHelper = httpHelper?? throw new ArgumentNullException(nameof(httpHelper));
        _consulServerManager = consulServerManager?? throw new ArgumentNullException(nameof(consulServerManager));
    }
    /// <summary>
    /// 获取服务地址
    /// </summary>
    /// <param name="serviceName"></param>
    /// <returns></returns>
    /// <exception cref="ArgumentNullException"></exception>
    private  Task<string> GetBaseUrl(string serviceName)
    {
        ArgumentException.ThrowIfNullOrEmpty(serviceName);
        return _consulServerManager.GetServerAddressAsync(serviceName);
    }
    /// <summary>
    /// 发起get请求
    /// </summary>
    /// <typeparam name="TResult"></typeparam>
    /// <param name="serviceName"></param>
    /// <param name="url"></param>
    /// <param name="parameters"></param>
    /// <returns></returns>
    public async  Task<TResult> GetAsync<TResult>(string serviceName, string url,
        Dictionary<string, string>? parameters = null)
    {
        var baseUrl = await GetBaseUrl(serviceName);
        return await _httpHelper.GetAsync<TResult>(string.Concat(baseUrl, url), parameters);
    }
    /// <summary>
    /// 发起post请求
    /// </summary>
    /// <typeparam name="TResult"></typeparam>
    /// <param name="serviceName"></param>
    /// <param name="url"></param>
    /// <param name="parameters"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public async Task<TResult> PostAsync<TResult>(string serviceName, string url,
        Dictionary<string, string>? parameters = null,object? data = null)
    {
        var baseUrl = await GetBaseUrl(serviceName);
        return await _httpHelper.PostAsync<TResult>(string.Concat(baseUrl, url), parameters, data);
    }
    /// <summary>
    /// 发起put请求
    /// </summary>
    /// <typeparam name="TResult"></typeparam>
    /// <param name="serviceName"></param>
    /// <param name="url"></param>
    /// <param name="parameters"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public async Task<TResult> PutAsync<TResult>(string serviceName, string url,
        Dictionary<string, string>? parameters = null, object? data = null)
    {
        var baseUrl = await GetBaseUrl(serviceName);
        return await _httpHelper.PutAsync<TResult>(string.Concat(baseUrl, url), parameters, data);
    }
    /// <summary>
    /// 发起delete请求
    /// </summary>
    /// <typeparam name="TResult"></typeparam>
    /// <param name="serviceName"></param>
    /// <param name="url"></param>
    /// <param name="parameters"></param>
    /// <returns></returns>
    public async Task<TResult> DeleteAsync<TResult>(string serviceName, string url,
        Dictionary<string, string>? parameters = null)
    {
        var baseUrl = await GetBaseUrl(serviceName);
        return await _httpHelper.DeleteAsync<TResult>(string.Concat(baseUrl, url), parameters);
    }

}